package com.ruoyi.account.service.impl;

import java.util.List;
import java.util.Map;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.*;
import com.ruoyi.db.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.account.service.IAccountItemChildService;
import com.ruoyi.common.core.text.Convert;

/**
 * 子账户 绑定信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-16
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class AccountItemChildServiceImpl implements IAccountItemChildService 
{
    @Autowired
    private AccountItemChildMapper accountItemChildMapper;
    @Autowired
    private AccountItemProductMapper accountItemProductMapper;
    @Autowired
    private AccountItemChannleMapper accountItemChannleMapper;
    @Autowired
    private ProductInfoMapper productInfoMapper;
    @Autowired
    private ChannelInfoMapper channelInfoMapper;

    /**
     * 查询子账户 绑定信息
     * 
     * @param id 子账户 绑定信息主键
     * @return 子账户 绑定信息
     */
    @Override
    public AccountItemChild selectAccountItemChildById(Long id)
    {
        return accountItemChildMapper.selectAccountItemChildById(id);
    }

    /**
     * 查询子账户 绑定信息列表
     * 
     * @param accountItemChild 子账户 绑定信息
     * @return 子账户 绑定信息
     */
    @Override
    public List<AccountItemChild> selectAccountItemChildList(AccountItemChild accountItemChild)
    {
        return accountItemChildMapper.selectAccountItemChildList(accountItemChild);
    }

    /**
     * 查询子账户 绑定信息列表
     *
     * @param accountItemChild 子账户 绑定信息
     * @return 子账户 绑定信息
     */
    @Override
    public List<Map<String, Object>> selectAccountItemChildListMap(AccountItemChild accountItemChild)
    {
        return accountItemChildMapper.selectAccountItemChildListMap(accountItemChild);
    }

    /**
     * 新增子账户 绑定信息
     * 
     * @param accountItemChild 子账户 绑定信息
     * @return 结果
     */
    @Override
    public int insertAccountItemChild(AccountItemChild accountItemChild)
    {
        if (StrUtil.equals(accountItemChild.getAccountType(), "PROD")){
            AccountItemProduct accountItemProduct =
                    accountItemProductMapper.selectAccountItemProductById(Long.valueOf(accountItemChild.getAccountId()));
            accountItemChild.setAccountName(accountItemProduct.getAccountName());

            ProductInfo productInfo = productInfoMapper.selectProductInfoById(Long.valueOf(accountItemChild.getDataId()));
            accountItemChild.setDataName(productInfo.getProductName());
        }else{
            AccountItemChannle accountItemChannle =
                    accountItemChannleMapper.selectAccountItemChannleById(Long.valueOf(accountItemChild.getAccountId()));
            accountItemChild.setAccountName(accountItemChannle.getAccountName());

            ChannelInfo channelInfo = channelInfoMapper.selectChannelInfoByChannelCode(accountItemChild.getDataId());
            accountItemChild.setDataName(channelInfo.getChannelName());
        }

        accountItemChild.setCreateTime(DateUtils.getNowDate());
        return accountItemChildMapper.insertAccountItemChild(accountItemChild);
    }

    /**
     * 修改子账户 绑定信息
     * 
     * @param accountItemChild 子账户 绑定信息
     * @return 结果
     */
    @Override
    public int updateAccountItemChild(AccountItemChild accountItemChild)
    {
        accountItemChild.setUpdateTime(DateUtils.getNowDate());
        return accountItemChildMapper.updateAccountItemChild(accountItemChild);
    }

    /**
     * 批量删除子账户 绑定信息
     * 
     * @param ids 需要删除的子账户 绑定信息主键
     * @return 结果
     */
    @Override
    public int deleteAccountItemChildByIds(String ids)
    {
        return accountItemChildMapper.deleteAccountItemChildByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除子账户 绑定信息信息
     * 
     * @param id 子账户 绑定信息主键
     * @return 结果
     */
    @Override
    public int deleteAccountItemChildById(Long id)
    {
        return accountItemChildMapper.deleteAccountItemChildById(id);
    }
}
